package models;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import util.DBAction;
import util.MyLogAction;

public class CommentData {

	private int commentID;
	private String developerName;
	private String comment;
	private int issueId;
	
	
	
	public String getDeveloperName() {
		return developerName;
	}

	public void setDeveloperName(String developerName) {
		this.developerName = developerName;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public int getIssueId() {
		return issueId;
	}

	public void setIssueId(int issueId) {
		this.issueId = issueId;
	}

	public int update() {
		String updateSQL = 
			    "update comments "
			    + "set issue_id = " + issueId + ",developer_name = '" + developerName + "',comment = '" + comment + "' " + 
				"where comment_id = " + commentID;
		return DBAction.update(updateSQL);
	}

	public int insert() {
		String insertSQL = "insert into comments "
				+ "(issue_id, developer_name, comment) " + "values(" + issueId
				+ ", '" + developerName + "', '" + comment + "');";
		return DBAction.update(insertSQL);
	}
	public static ArrayList<CommentData> find(String selectionModifier) {
		ArrayList<CommentData> selection = new ArrayList<CommentData>();
		ResultSet resultSet = null;
		String query = "select * "+
				"from comments " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			while (resultSet.next()) {
				CommentData cmnt = new CommentData();
				cmnt.commentID=resultSet.getInt("comment_id");
				cmnt.issueId=resultSet.getInt("issue_id");
				cmnt.developerName=resultSet.getString("developer_name");
				cmnt.comment=resultSet.getString("comment");
				selection.add(cmnt);
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("CommentData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return selection;
	}

	public static CommentData findOne(String selectionModifier) {
		ResultSet resultSet = null;
		String query = "select * "+
				"from comments " + selectionModifier;
		Connection connection = DBAction.getConnection();
		resultSet = DBAction.readFromDB(query, connection);
		try {
			if (resultSet.next()) {
				CommentData cmnt = new CommentData();
				cmnt.commentID=resultSet.getInt("comment_id");
				cmnt.issueId=resultSet.getInt("issue_id");
				cmnt.developerName=resultSet.getString("developer_name");
				cmnt.comment=resultSet.getString("comment");
				DBAction.close(resultSet);
				DBAction.close(connection);
				return cmnt;
			}
		} catch (SQLException e) {
            MyLogAction.myCatch("CommentData.java", 43, e);
			e.printStackTrace();
		}
		DBAction.close(resultSet);
		DBAction.close(connection);
		return null;
	}
	
}

